#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
static int SCREENX,SCREENY;

#define CELLX 30
#define CELLY 30

#define GRIDX 12
#define GRIDY 12

#define GRID_COLOR 15
#define HC 12

static int sys[GRIDX][GRIDY];
static int high[GRIDX][GRIDY];

#define SYS_SPREAD 20

void init_arr()
{
	int i,j;
	for(j=0;j<GRIDY;j++)	{
		for(i=0;i<GRIDX;i++)	{
			sys[i][j]=0;
			high[i][j]=0;
		}
	}
}

void draw_grid()
{
	int i,j;
	setcolor(GRID_COLOR);
	for(j=0;j<GRIDY;j++)	{
		for(i=0;i<GRIDX;i++)	{
			rectangle(i*CELLX,j*CELLY,(i+1)*CELLX,(j+1)*CELLY);
		}
	}
}

void color_cell(int x,int y,int color)
{
	setfillstyle(INTERLEAVE_FILL,color);
	bar(x*CELLX+1,y*CELLY+1,x*CELLX+CELLX-1,y*CELLY+CELLY-1);
}

void decol_cell(int x,int y)
{
	setfillstyle(SOLID_FILL,0);
	bar(x*CELLX+1,y*CELLY+1,x*CELLX+CELLX-1,y*CELLY+CELLY-1);
}

void num_cell(int x,int y,int n)
{
	char ns[10];
	outtextxy(x*CELLX+2,y*CELLY+5,itoa(n,ns,10));
}

void num_cell2(int x,int y,int n)
{
	char ns[10];
	outtextxy(x*CELLX+2,y*CELLY+15,itoa(n,ns,10));
}

int vect_dist(int x,int y,int x1,int y1)
{
	return ((x-x1)*(x-x1)+(y-y1)*(y-y1));
}

void update_high()
{
	int i,j;
	for(j=0;j<GRIDY;j++)	{
		for(i=0;i<GRIDX;i++)	{
			if(high[i][j]>1)	{
				color_cell(i,j,HC);
				num_cell(i,j,high[i][j]);
				high[i][j]/=2;
			} else	{
				color_cell(i,j,0);
			}
		}
	}
}

void update_spread()
{
	int i,j,k,l;
	for(j=0;j<GRIDY;j++)	{
		for(i=0;i<GRIDX;i++)	{
			for(l=0;l<GRIDY;l++)	{
				for(k=0;k<GRIDX;k++)	{
					if(vect_dist(i,j,k,l)+SYS_SPREAD)
						sys[k][l]=sys[i][j];
				}
			}
		}
	}
}

void main()
{
	int i,j,gdriver=DETECT,gmode;
	initgraph(&gdriver,&gmode,"");
	SCREENX=getmaxx();
	SCREENY=getmaxy();
	init_arr();
	draw_grid();
	getch();
	high[2][3]=200;
	while(!kbhit())	{
		update_high();
		update_spread();
	}
	closegraph();
}